package com.tamir.template.context;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author : tanggh
 * @description : 模版解析上下文
 * @date : 2016/10/26
 */
public class TemplateContext
{
    /**
     * 请求参数集合
     */
    private Map<String, Object> requestMap = null;
    private Map<String, Object> bakRequestMap = null;
    public TemplateContext(Map<String,Object> requestMap){
        this.bakRequestMap = requestMap;
        setRequestMap(requestMap);
    }

    public Map<String, Object> getRequestMap()
    {
        return requestMap;
    }

    public void setRequestMap(Map<String, Object> requestMap)
    {
        this.bakRequestMap = requestMap;
        if(requestMap != null){
            this.requestMap = new HashMap<String,Object>();
            Set<Map.Entry<String,Object>> entries = requestMap.entrySet();
            //转换数字类型为BigDecimal,方便后续做比较
            for (Map.Entry<String,Object> entry : entries){
                Object val = entry.getValue();
                if(val instanceof Byte
                        || val instanceof Short || val instanceof Integer || val instanceof Long){
                    this.requestMap.put(entry.getKey(), new BigDecimal(val.toString()));
                }else if(val instanceof Double){
                    this.requestMap.put(entry.getKey(), new BigDecimal(val.toString()));
                }else{
                    this.requestMap.put(entry.getKey(), entry.getValue());
                }
            }
        }
    }

    /**
     * 获取请求参数的值
     * @param key
     * @return
     */
    public Object getObject(String key){
        return requestMap.get(key);
    }
}
